package com.entidades;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.graphics.Bitmap;

import com.CustomClasses.FetchImage;
import com.CustomClasses.FetchXML;


public class News {
	
	public String getmEnclosure() {
		return mEnclosure;
	}


	
	 static class Category{
		 String Area;
		 String Section;
		 String Kind;
		 String Tornament;
	 }


	private static final String[] fieldNames = new String[]{"title","link", "description", "enclosure", "pubDate"};
	private static final String tagName = "item";
	
	private static final int LIST_TITLE = 0;
	private static final int LIST_LINK = 1;
	private static final int LIST_ENCLOSURE = 3;
	
	private static List<News> genNews;
	private String mTtitle;
	private String mLink;
	private String mDescription;
	private String mEnclosure;
	private Date mPublicationDate;
	private Category mCategory;
	private Bitmap image;
	
	public String getmTtitle() {
		return mTtitle;
	}

	public void setmTtitle(String mTtitle) {
		this.mTtitle = mTtitle;
	}

	public String getmLink() {
		return mLink;
	}



	public String getmDescription() {
		return mDescription;
	}

	public void setmDescription(String mDescription) {
		this.mDescription = mDescription;
	}



	public Date getmPublicationDate() {
		return mPublicationDate;
	}

	public void setmPublicationDate(Date mPublicationDate) {
		this.mPublicationDate = mPublicationDate;
	}

	
	public Category getmCategory() {
		return mCategory;
	}

	public void setmCategory(Category mCategory) {
		this.mCategory = mCategory;
	}

	private News(String ttitle, String link, String enclosure, Date publicationDate){
		mTtitle = ttitle;
		mLink = link;
		mEnclosure = enclosure;
		mPublicationDate = publicationDate;
		new FetchImage().execute(this);
		
	}
	
	public static List<News> getNews(String preURL, String posURL){

		List<News> tempNews = new ArrayList<News>(); 
			try {
				FetchXML fetch =  new FetchXML();
				List<List<String>> list = fetch.execute(preURL+posURL, tagName, fieldNames).get();
				for(List<String> l: list){	
					
					Date publicationDate = new Date();
					
					String link = new String(l.get(LIST_LINK));
					if(!link.startsWith(preURL)){
						link = new String(preURL + l.get(LIST_LINK));
					}
					tempNews.add(new News(
							l.get(LIST_TITLE),
							link,
							l.get(LIST_ENCLOSURE),
							publicationDate));
					}	
				}  catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		return tempNews;
		
	}
	
	public static List<News> getNewsGeneral(String preURL, String posURL){
		if(genNews==null){
			genNews = getNews(preURL, posURL);
		}
		return genNews;
		
	}
	
	public Bitmap getImage() {
		return image;
	}

	public void setImage(Bitmap image) {
		this.image = image;
	}
	



}
